package cn.yitun.chat.yitunchatbasesdk.config;

import cn.yitun.chat.yitunactiondispatch.ActionDispatcher;
import cn.yitun.chat.yitunactiondispatch.config.ActionConfigurer;
import cn.yitun.chat.yitunactiondispatch.extend.ActionBox;
import cn.yitun.chat.yitunactiondispatch.registry.ActionBoxRegistry;
import cn.yitun.chat.yitunactiondispatch.registry.ActionInterceptorRegistry;
import cn.yitun.chat.yitunchatbasesdk.server.im.dispatcher.impl.HandlerActionInterceptor;
import cn.yitun.chat.yitunchatbasesdk.server.im.dispatcher.impl.HandlerAuthInterceptor;
import cn.yitun.chat.yitunjsonvalidator.ValidatorService;

/**
 * Date 2019-04-20 08:13:26<br>
 * Description
 * @author yitun
 * @since 1.1
 */
public class HandlerActionDispatcher extends ActionDispatcher {

	HandlerActionConfigurer handlerActionConfigurer = new HandlerActionConfigurer();
	HandlerActionInterceptor handlerActionInterceptor = new HandlerActionInterceptor(this);
	HandlerAuthInterceptor handlerAuthInterceptor = new HandlerAuthInterceptor();

	public HandlerActionDispatcher() {
		this.init();
	}

	public HandlerActionDispatcher(ActionBox actionBox) {
		this.init();
		this.add(actionBox);
	}

	public HandlerActionDispatcher(ActionBox actionBox, String... actionLocations) {
		this.init();
		this.add(actionBox);
		this.scan(actionLocations);
	}

	public void addActionValidator(String actionValidatorConfigPaht) {
		// "classpath*:/validator/main/action/*.json"
		ValidatorService validatorService = new ValidatorService(actionValidatorConfigPaht);
		handlerActionInterceptor.setValidatorService(validatorService);
	}

	public void addConfig(ActionConfigurer actionConfigurer) {
		super.addConfig(actionConfigurer);
	}

	private void init() {

		this.addConfig(handlerActionConfigurer);
		this.addConfig(new HandlerActionConfigurerAdapter() {
			@Override
			public void addActionInterceptor(ActionInterceptorRegistry registry) {
				registry.add(handlerActionInterceptor);
				registry.add(handlerAuthInterceptor);
			}
		});
	}

	private void add(ActionBox actionBox) {
		// "com.oim.server.general.main.business.*.action"
		this.addConfig(new HandlerActionConfigurerAdapter() {
			@Override
			public void addActionBox(ActionBoxRegistry registry) {
				registry.add(actionBox);
			}
		});
	}
}
